package edu.hunau.recommend.controller;

import edu.hunau.recommend.common.CommonUtils;
import edu.hunau.recommend.common.R;
import edu.hunau.recommend.common.ResultCodeEnum;
import edu.hunau.recommend.exception.RecommendBusinessException;
import edu.hunau.recommend.pojo.User;
import edu.hunau.recommend.request.LoginReq;
import edu.hunau.recommend.request.RegistReq;
import edu.hunau.recommend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @author name
 * @creater 2022-09-08-20:43
 */
@RestController
@RequestMapping("/user")
public class UserManagerAction {

    private static final String SESSION_USER="SESSION_USER_KEY";

    @Autowired
    private UserService userService;

    //queryString: http://localhost:8080/user/findByid?id = 1
    //RESTfull: get http://localhost:8080/user/1
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public R queryById(@PathVariable("id") Integer id){
        User u= userService.findById(id);
//        if (2>1){
//            throw new NullPointerException("测试空异常");
//        }
        return R.ok().data("user",u);
    }

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("/index.html");
        String username = "haoren";
        mv.addObject("name",username);
        return mv;
    }

    /**
     * 处理用户的注册
     * 如果校验失败, SpringBoot会将异常信息绑定到BindingResult中
     * @return
     */
    @PostMapping("/regist")
    @ResponseBody
    public R register(@Valid RegistReq register, BindingResult result){

        //判断是否校验失败
        if(result.hasErrors()){


            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR, CommonUtils.processErrorMessage(result));
        }

        //如果合法, 进行处理
        User user = new User();
        user.setPassword(register.getPassword());
        user.setNickName(register.getNickName());
        user.setTelphone(register.getTelphone());

        User u = userService.register(user);
        return R.ok().data("user",u);
    }

    @Autowired
    private HttpServletRequest servletRequest;

    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody LoginReq loginReq, BindingResult result){
        if(result.hasErrors()){

            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR, CommonUtils.processErrorMessage(result));
        }
        User u = userService.login(loginReq.getTelphone(), loginReq.getPassword());
        //将用户信息封装到Session中
        servletRequest.getSession().setAttribute(SESSION_USER,u);
        return R.ok().data("user",u);
    }

    /**
     * 登出
     * @return
     */
    @GetMapping("/logout")
    public R logout(){
        //将session中的数据撤销
        servletRequest.getSession().invalidate();
        return R.ok().data("user",null);
    }

    /**
     * 从session中获得用户数据
     * @return
     */
    @PostMapping("/getCurrentUser")
    public R getCurrentUser(){
        User user = (User)servletRequest.getSession().getAttribute(SESSION_USER);
        return R.ok().data("user",user);
    }
}
